module.exports = function(sequelize, DataTypes) {
  const hexUtil = require('../lib/hex')

  var DocLink = sequelize.define(
    'DocLink',
    {
      sourceId: {
        type: 'BINARY(16)',
        get() {
          let uuid = this.getDataValue('sourceId')
          if (!uuid) return null
          return hexUtil.bytesToHex(uuid)
        },
        set(val) {
          this.setDataValue('sourceId', hexUtil.hexToBytes(val))
        }
      },
      targetId: {
        type: 'BINARY(16)',
        get() {
          let uuid = this.getDataValue('targetId')
          if (!uuid) return null
          return hexUtil.bytesToHex(uuid)
        },
        set(val) {
          this.setDataValue('targetId', hexUtil.hexToBytes(val))
        }
      }
    },
    {
      tableName: 'doc_links',
      indexes: [
        {
          name: 'source_id_idx',
          fields: ['sourceId']
        },
        {
          name: 'target_id_idx',
          fields: ['targetId']
        }
      ],
      comments: 'Document links association table'
    }
  )

  return DocLink
}
